/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.view.freemarker.model;

import java.util.ArrayList;
import java.util.List;

import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

import org.extremecomponents.table.core.TableModel;

/**
 * @author Eccn-sshwsfc
 * 
 * a model for extend table's bean.
 */
public abstract class TemplateExtendModel implements TemplateHashModel {

	protected TableModel model;
	protected List beans = new ArrayList();
	protected BeansWrapper beanWrapper = new BeansWrapper();

	public TemplateModel get(String key) throws TemplateModelException {
		if(beans == null){
			return null;
		}
		TemplateModel o = getByKey(key);
		if(o != null){
			return o;
		}
		
		for(int i=0;i<beans.size();i++){
			Object bean = beans.get(i);
			if(BeanModel.class.isAssignableFrom(bean.getClass())){
				TemplateModel temp = ((BeanModel)bean).get(key);
				if (temp != null && temp instanceof TemplateModel) {
					return temp;
				}
			}
		}
		return null;
	}
	
	protected TemplateModel getByKey(String key) throws TemplateModelException {
		return null;
	}

	public boolean isEmpty() throws TemplateModelException {
		return false;
	}

}
